package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashAccount;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TransactionSettlement;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TransactionSettlementTwo;

import java.util.List;

/**
 * 功能:交易结算数据层接口类
 * 编写者：
 * 日期：2021
 * 版本：1.0
 */
public interface TrabsactionDao {
    /**
     * 分页查询条件
     * @param start
     * @param rows
     * @param condition
     * @return
     */
    @Select("select s.tranId,s.tranDate,s.tranNum,s.price,s.settMoney,s.settDate,s.tranMoney,c.accName,fs.seName,se.stockName,s.may,s.tranStatus from securitiesTradingData s " +
            "join settingOfSecurities se on s.stockId=se.stockId join financialsecurities fs on s.seId=fs.seId " +
            "join cashAccount c on s.caId=c.caId where 1=1 ${condition} and to_char(s.tranDate,'yyyy-MM-dd')=to_char(sysdate,'yyyy-MM-dd') and s.tranStatus=-1")
    public List<TransactionSettlementTwo> findByPage(@Param("start") int start, @Param("rows") int rows, @Param("condition") String condition);

    /**
     * 按条件统计记录数
     * @param condition 统计条件
     * @return 条件
     */
    @Select("select count(*) from securitiesTradingData s join settingOfSecurities se on s.stockId=se.stockId join " +
            "financialsecurities fs on s.seId=fs.seId join cashAccount c on s.caId=c.caId where 1=1 ${condition}")
    public int count(@Param("condition") String condition);


    /**
     * 根据证券交易编号删除数据
     * @param tranId 证券交易编号
     * @return 1：删除成功；0：删除失败
     */
    @Delete("delete from securitiesTradingData where s.tranId=#{tranId}")
    public int delete(@Param("tranId") int tranId);

    /**
     * 修改交易数据
     * @param transactionSettlement 交易数据对象
     * @return 1：修改成功；0：修改失败
     */
    @Update("update securitiesTradingData set tranStatus=#{tranStatus} where tranId=#{tranId}")
    public int update(TransactionSettlement transactionSettlement);

    /**
     * 查询所有交易处理数据
     * @return 交易处理数据对象集合
     */
    @Select("select * from securitiesTradingData where to_char(tranDate,'yyyy-MM-dd')=to_char(sysdate,'yyyy-MM-dd') and tranId=#{tranId} ")
    public TransactionSettlement findById(@Param("tranId") int tranId);

    @Select("select * from CashAccount")
    public List<CashAccount> queryCash();

    /**
     * 通过证券编号判断是否为债券
     * @return
     */
    @Select("select count(*) from financialsecurities where seId=#{seId} and seType=2")
    public int ifIsBond(@Param("seId") String seId);

    /**
     * 通过债券编号从证券库存中查询该债券数量
     * @param seId
     * @return
     */
    @Select("select secNum from securitiesStock where seId=#{seId}")
    public int bondRepositoryCount(@Param("seId") String seId);

    /**
     * 通过证券编号从证券应收应付取得数据条数
     * @param seId
     * @return
     */
    @Select("select count(*) from receivableAndPayable where businType='债券利息' and seId=#{seId}")
    public int getBondCount(@Param("seId") String seId);

    /**
     * 过证券编号从证券应收应付取得所有债券利息
     * @param seId
     * @return
     */
    @Select("select nvl(sum(money),0) from receivableAndPayable where businType='债券利息' and seId=#{seId}")
    public Double getBondEveryMoney(@Param("seId") String seId);

    /**
     * 修改证券应收应付中对应该债券每天的利息：减去卖掉的债券利息
     * @param EveryDayMoney
     */
    @Update("update receivableAndPayable set money=money-#{EveryDayMoney} where businType='债券利息' and seId=#{seId}")
    public void updateBondEveryDayMoney(@Param("EveryDayMoney") Double EveryDayMoney,@Param("seId") String seId);

    /**
     * 在证券应收应付中删除金额为0并且类型为债券利息的数据
     * @param seId
     */
    @Delete("delete from receivableAndPayable where businType='债券利息' and money=0")
    public void clearBond();

}
